from threading import Thread, Lock

a = 0


def func():
    global a
     # 在对全局变量a取值操作之前，给线程上锁
    lock.acquire()
    for i in range(100000):
        a += 1
     # 操作完全局变量，解锁
    lock.release()

if __name__ == '__main__':
    lock = Lock()
    thread1 = Thread(target=func)
    thread2 = Thread(target=func)
    thread1.start()
    thread2.start()
    thread1.join()
    thread2.join()
    print(a)
